<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Tree {
	protected $listarr = array();
	protected $ret = '';
	protected $liststr = '<option value="$id" $selected $disabled >$spacer $title</option>';
	protected $liststrarr = array(
				'option'=>'<option value="$id" $selected $disabled >$spacer $title</option>',
			  );
	protected $icon = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	protected $pid = 0;
	protected $tid = '';
	protected $did = '';

	protected $parentkey='parent';

	protected $depth=-1;

	protected $model=null;
	protected $order='`order` ASC,`id` ASC';
	protected $where=array();

	protected $filter='';

	public function __construct($arr = array()){
		if(is_array($arr)){
			if(isset($arr['listarr'])){
				$this->listarr = $arr['listarr'];
			}
			if(isset($arr['liststr'])){
				$this->liststr = $arr['liststr'];
			}
			if(isset($arr['option'])){
				$this->liststr = $this->liststrarr[$arr['liststr']];
			}
			if(isset($arr['icon'])){
				$this->icon = $arr['icon'];
			}
			if(isset($arr['pid'])){
				$this->pid = $arr['pid'];
			}
			if(isset($arr['tid'])){
				$this->tid = $arr['tid'];
			}
			if(isset($arr['did'])){
				$this->did = $arr['did'];
			}
			if(isset($arr['model'])){
				$this->model=$arr['model'];
			}
			if(isset($arr['where'])){
				$this->where=$arr['where'];
			}
			if(isset($arr['order'])){
				$this->order=$arr['order'];
			}
			if(isset($arr['filter'])){
				$this->filter=$arr['filter'];
			}
			if(isset($arr['depth'])){
				$this->depth=$arr['depth'];
			}
			if(isset($arr['parentkey'])){
				$this->parentkey=$arr['parentkey'];
			}
			$this->ret = '';
		}else{
			show_error(lang('message'),200,lang('treeerror'));
		}
	}
	
	public function getlist($depth=NULL){
		if(!is_null($depth))$this->depth=$depth;
		return $this->get_tree($this->pid,$this->liststr,$this->tid,$this->did);
	}
	
	private function get_tree($pid,$str,$tid='',$did='',$level=0, $spacer=''){
		if($this->depth>=0 && $level>= $this->depth)return;
		$child = $this->get_child($pid);

		if(is_array($child)){
			$len=count($child);
			foreach($child as $key => $item){
				if(empty($keys)){
					$keys=array_map(create_function('$item', 'return \'{$\'.$item.\'}\';'), array_keys($item)) ;
					$keys[]='{$selected}';
					$keys[]='{$disabled}';
					$keys[]='{$spacer}';
				}
				$item['selected'] = $item['id']==$tid ? 'selected' : '';
				$item['disabled'] = $item['id']==$did ? 'disabled' : '';
				if($key == 0){
					if($len==1){
						$item['spacer']=$spacer.'┗';
						$cpre=$spacer.'　';
					}else{
						$item['spacer']=$spacer.'┣';
						$cpre=$spacer.'┃';
					}
				}elseif($key==$len-1){
					$item['spacer']=$spacer.'┗';
					$cpre=$spacer.'　';
				}else{
					$item['spacer']=$spacer.'┣';
					$cpre=$spacer.'┃';
				}
				$this->ret .= @str_replace($keys, $item, $str);//$newstr;
				$this->get_tree($item['id'],$str,$tid,$did,$level+1, $cpre);
			}
		}
		return $this->ret;
	}
	
	private function get_child($pid){
		if($this->model){
			$newarr=$this->model->Data_model->getData(array_merge($this->where,array($this->parentkey=>$pid)), $this->order);
			if($this->filter){
				if($this->filter=='data'){
					$newarr=$this->model->dataFilter($newarr);
				}else{	//item
					for($i=0;$i<count($newarr);$i++){
						$newarr[$i]=$this->model->itemFilter($newarr[$i]);
					}
					
				}
				
			}
		}else{
			$newarr = array();
			foreach($this->listarr as $key => $item){
				if($item[$this->parentkey]==$pid){
					$newarr[] = $item;
				}
			}
		}
		
		return $newarr?$newarr:false;
	}
	
	
}
